<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
          <div class="search-btn search-btns" @click.stop="queryPageList()">
            <i class="el-icon-circle-check"></i>
            <p>查询</p>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">处理人</div>
            <el-select filterable clearable v-model="staffName" placeholder="请选择" filterable clearable>
              <el-option v-for="(item, index) in staffList" :key="index + 'staff'" :label="item.staffName"
                :value="item.staffName" />
            </el-select>
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:btnsBar>
      <CategoryBar :categoryList="categoryList" @categoryBar="categoryBar" />
    </template>
    <template v-slot:categoryBox></template>
    <template v-slot:content>
      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" stripe border
        height="calc(100% - 50px)" :row-key="row => {
          return row.fishOutTaskId;
        }
          " @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <el-table-column type="selection" width="55" :reserve-selection="true"
          :selectable="(row) => { return !row.selectable }" />
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">{{ scoped.row.fishOutTaskId ? scoped.$index + 1 : '' }}</template>
        </el-table-column>
        <el-table-column prop="fishOutTaskName" label="出鱼任务" />
        <el-table-column prop="staffName" label="任务创建人" />
        <el-table-column prop="completeFlag" label="是否完成">
          <template v-if="scoped.row.fishOutTaskId" slot-scope="scoped">
            <div v-if="scoped.row.auditType === 'PASS'">
              <el-switch :disabled="scoped.row.completeFlag" v-model="scoped.row.completeFlag"
                @change="switchChange(scoped.row)" active-text="是" inactive-text="否" />
            </div>
            <div v-else>
              {{ scoped.row.completeFlag ? "已完成" : "未完成" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="codeNumber" label="产出码单号" />
        <el-table-column v-if="auditType !== 'REVIEW'" prop="deadDate" label="审核结果">
          <template v-if="scoped.row.fishOutTaskId" slot-scope="scoped">
            <div v-if="scoped.row.auditType === 'PASS'">
              <p>通过审核</p>
            </div>
            <div v-if="scoped.row.auditType === 'NOT_PASS'">
              <p>未通过审核</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template v-if="scoped.row.fishOutTaskId" slot-scope="scoped">
            <div class="table-btns-box">
              <div class="table-btns edit-btns" @click="completeInfo(scoped.row)">
                <p>任务详情</p>
              </div>
              <div v-if="auditType === 'REVIEW'" class="table-btns edit-btns" @click="pass(scoped.row)">
                <p>通过</p>
              </div>
              <div v-if="auditType === 'REVIEW'" class="table-btns edit-btns" @click="unPass(scoped.row)">
                <p>不通过</p>
              </div>
              <div v-if="scoped.row.auditType === 'NOT_PASS'" class="table-btns edit-btns"
                @click="showUnpassReason(scoped.row)">
                <p>查看未通过原因</p>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="'未通过原因'" :visible.sync="showDialog" width="25%" top="0" :close-on-click-modal="false"
        @close="cancel">
        <el-form label-position="left" class="el-form">
          <el-form-item label="不通过原因: " prop="failReason" class="w100p">
            <el-input type="textarea" :rows="3" placeholder="请输入内容" disabled v-model="editObject.failReason"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="cancelbtn" @click="showDialog = false">取 消</el-button>
          <el-button type="primary" class="savebtn" @click="showDialog = false">保 存</el-button>
        </div>
      </el-dialog>
      <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
        @changePageSize="changePageSize" @changeCurrentPage="changeCurrentPage" />
      <passUnpassDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :edit-object="editObject"
        :show.sync="showAddEditDialog" @closeDialog="closeDialog" @cancelDialog="cancel" />
      <CompleteInfoDialog ref="completeInfoDialog" :now-chance-stau="nowChanceStau"
        :complete-info-object="completeInfoObject" :show.sync="showCompleteInfoDialog" @closeDialog="closeDialog"
        @cancelDialog="cancel" />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import CategoryBar from "@/components/CategoryBar";
import Pagination from "@/components/Pagination";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new2.vue";
import passUnpassDialog from "./components/pass-unpass-dialog.vue";
import CompleteInfoDialog from "./components/complete-info-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import FishOutTaskApi from "@/api/output-manage/fish-out-task";
import StaffApi from "@/api/basic-info-manage/staff";
import BatchApi from "@/api/batch-mange/index";
import FishPondApi from "@/api/basic-info-manage/fishPond";
import EnumApi from "@/api/enum";
export default {
  name: "StaffManage",
  components: {
    ActionBar,
    Pagination,
    BaseMainSlot,
    passUnpassDialog,
    CompleteInfoDialog,
    CategoryBar
  },
  mixins: [mymixin],
  data () {
    return {
      // 查询条件：单位名称
      auditType: undefined,
      staffName: "", // 处理人
      deadDate: "", // 直鱼日期
      productionBatchId: undefined, // 批次ID
      fishPondId: undefined, // 鱼塘ID
      staffList: undefined,
      batchList: undefined,
      pondList: undefined,
      checkedIdList: [],
      categoryList: [],
      showDialog: false,
      completeInfoObject: {},
      showCompleteInfoDialog: false
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created () {
    // this.queryPageList();
    this.queryEnum();
    this.queryStaff();
    this.queryBatch()
    this.queryFishPond()
  },
  mounted () { },
  methods: {

    switchChange (row) {
      let flag = !row.completeFlag
      console.log(flag)
      this.$confirm('此任务完成后无法再次变更, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        FishOutTaskApi.fishOutTask({ 'id': row.fishOutTaskId }).then(res => {
          this.$message({
            type: 'success',
            message: '已完成!'
          });
          row.completeFlag = !flag
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
        row.completeFlag = flag
      });
    },
    categoryBar (item) {
      this.auditType = item.value
      this.queryPageList();
      this.$nextTick(() => {
        this.$refs.tableData.doLayout()
      })
    },
    queryBatch () {
      BatchApi.queryByCondition({}).then(res => {
        this.batchList = res.data
      })
    },
    queryFishPond () {
      FishPondApi.queryByCondition({}).then(res => {
        this.pondList = res.data;
      })
    },
    queryStaff () {
      StaffApi.queryByCondition().then(res => {
        this.staffList = res.data;
      });
    },
    queryEnum () {
      EnumApi.list({ enumType: "AuditType" }).then(res => {
        this.categoryList = []
        res.data.forEach((item, index) => {
          this.categoryList.push({
            id: index,
            name: item.value,
            value: item.name
          })
        });
      })
    },
    queryPageList () {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {}
      };
      if (this.auditType) params.entity.auditType = this.auditType;
      if (this.productionBatchId) params.entity.productionBatchId = this.productionBatchId;
      if (this.fishPondId) params.entity.fishPondId = this.fishPondId;
      if (this.staffName) params.entity.staffName = this.staffName;
      if (this.deadDate) params.entity.deadDate = this.deadDate;
      FishOutTaskApi.queryPageByCondition(params).then(res => {
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // 点击详情
    completeInfo (row) {
      if (row.fishOutTaskId) {
        this.completeInfoObject = JSON.parse(JSON.stringify(row));
        this.showCompleteInfoDialog = true;
      }
    },
    // 表格删除
    delmessage () {
      FishOutTaskApi.delListById({ idList: this.checkedIdList }).then(res => {
        if (res.success) {
          this.$message.success("删除成功");
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    changePageSize (pageSize) {
      this.pageSize = pageSize;
      this.queryPageList();
    },
    changeCurrentPage (currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    selectAll (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.deadRecordId) {
          notvalue = false;
        }
      });
      // if (notvalue === false) {
      //   this.$message.warning("不可选择空数据！");
      // }
    },
    select (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.deadRecordId) {
          notvalue = false;
        }
      });
      // if (notvalue === false) {
      //   this.$message.warning("不可选择空数据！");
      // }
    },
    toggleNoSelection (rows) {
      if (rows) {
        this.$nextTick(() => {
          rows.forEach(row => {
            this.$refs.tableData.toggleRowSelection(row, false);
          });
        })
      } else {
        this.$refs.tableData.clearSelection();
      }
    },
    handleSelectionChange (list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.deadRecordId) {
          return item.deadRecordId;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      // if (notvalue === false) {
      //   // this.$message.warning("不可选择空数据！");
      //   this.toggleNoSelection(emptyList);
      //   return;
      // }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 展示查看未通过原因弹窗
    showUnpassReason (row) {
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showDialog = true;
    },
    // 审核
    pass (row) {
      this.nowChanceStau = "pass";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    unPass (row) {
      if (row.fishOutTaskId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "unpass";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
    cancel () {
      this.nowChanceStau = '';
      this.titleText = '';
      this.editObject = {};
      this.showAddEditDialog = false;
      this.addActive = true;
      this.delActive = false;
      this.showCompleteInfoDialog = false;
      this.$refs.tableData.setCurrentRow()
      this.$refs.tableData.clearSelection()
    },
  }
};
</script>

<style lang="scss" scoped></style>
